home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
COMMUNIC
/
TERMINAL
/
1589.ZIP
/
TERMNRES.C
< prev
next >
Wrap
Text File
|
1989-04-13
|
20KB
|
587 lines
/*****************************************************************************
** The following functions are called from the TermWndProc() function.
**
*****************************************************************************/
#include "windows.h"
#include "term.h"
#include "icu_user.h"
TermCreate( hWnd )
HWND hWnd;
{
int h, w;
TEXTMETRIC tm;
hTermDC = GetDC (hWnd); /* get display context */
/* Add fonts to Windows resource. Needed for IBM graphic characters */
if ( !(ret = AddFontResource( (LPSTR) "FONTRES.FON" )) )
pmsg("Unable to add FONTRES fonts to WINDOWS.");
else
SendMessage( -1, WM_FONTCHANGE, 0, 0L );
if ( !(ret = AddFontResource( (LPSTR) "IBM.FON" )) )
pmsg("Unable to add IBM fonts to WINDOWS.");
else
SendMessage( -1, WM_FONTCHANGE, 0, 0L );
IBMfont = CreateFont( 14, 12,0,0,0,0,0,0,
OEM_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH|FF_DONTCARE, (LPSTR) "IBM" );
if ( IBMfont == NULL ) { /* Unable to load font */
pmsg("Unable to select IBM style font.");
IBMfont = GetStockObject( SYSTEM_FONT );
}
SelectObject( hTermDC, IBMfont );
GetTextMetrics( hTermDC, (TEXTMETRIC FAR *)&tm); /* text size */
char_w = tm.tmAveCharWidth; /* char width */
char_h = tm.tmHeight + tm.tmExternalLeading; /* char height */
Capture = FALSE; /* Flags, etc, used in the capture routines */
CaptureFD = -1; /* " " " " " " " */
CaptureLine = 1; /* " " " " " " " */
curs_pos.x = char_w; /* Current X Caret position */
curs_pos.y = 1; /* Current Y Caret position */
next_line = 0; /* Offset for the next line in the display */
next_char = 0; /* Offset for the next col. in the display */
GraphicsMode = FALSE; /* Turn graphics mode off */
CaretMode = TRUE; /* Turn the caret on */
TermHasFocus = TRUE; /* Set when TERMINAL has the focus */
PaintWindow = TRUE; /* Set after a scroll to prevent a repaint */
}
TermCharInput(hWnd, ch, cRepeat)
HWND hWnd;
int ch;
int cRepeat;
{
int i, ret;
while (cRepeat-- != 0) {
if ( ch < 0x20 ) { /* A CONTROL character, send at once */
if ( ch != CR && ch != LF )
fileput("Sending CONTROL character %XH", ch );
TransmitCommChar( nCID, ch );
}
else {
for (i=0,ret=-1;i<5,ret<0;i++) /* Try to send character five times */
ret = WriteComm( nCID, (LPSTR)&ch, 1 );
if (ret<0) { /* ERROR xmting */
fileput("Transmit Error #%d", ret );
DisplayCommError();
}
}
}
}
TermClose(hWnd)
HWND hWnd;
{
char szOkClose[CCHMAXSTRING], szClose[CCHMAXSTRING];
LoadString(hInstTerm, IDSOKCLOSE, (LPSTR)szOkClose, CCHMAXSTRING);
LoadString(hInstTerm, IDSCLOSE, (LPSTR)szClose , CCHMAXSTRING);
MessageBeep(MB_OKCANCEL | MB_ICONQUESTION);
if (MessageBox(hWnd, (LPSTR)szOkClose, (LPSTR)szClose,
MB_OKCANCEL | MB_ICONQUESTION) == IDOK) {
DestroyWindow(hWnd);
CloseComm(nCID);
}
}
TermEndSession(hWnd, bEnding)
HWND hWnd;
BOOL bEnding;
{
if (bEnding) { /* really ending */
if ( Capture == TRUE )
CloseCapture();
if ( nCID )
CloseComm(nCID);
nCID = NULL;
LocalUnlock( hCommHandle ); /* Unlock and free the DCB Buffer */
LocalFree( hCommHandle );
DestroyCaret();
RemoveFontResource( (LPSTR) "FONTRES.FON" ); /* Remove the Fonts */
RemoveFontResource( (LPSTR) "IBM.FON" );
DeleteObject( IBMfont );
SelectObject(hTermDC,GetStockObject(SYSTEM_FONT));/* Get system font */
ReleaseDC(hWndTerm,hTermDC);
}
else
{
/* someone replied FALSE to WM_QUERYENDSESSION */
}
}
TermEraseBkgnd(hWnd, hDC) /* Disabled in termres.c */
HWND hWnd;
HDC hDC;
{
HBRUSH hbr, hbrOld;
RECT rect;
GetClientRect(hWnd, (LPRECT)&rect);
hbrOld = SelectObject( hDC, hbrWhite );
FillRect(hDC, (LPRECT)&rect, hbr);
SelectObject(hDC, hbrOld);
}
TermKeyInput(hWnd, message, ch)
HWND hWnd;
unsigned message;
int ch;
{
switch (message) {
case WM_KEYDOWN:
TranslateKey( ch );
break;
case WM_KEYUP:
break;
}
}
TermGetFocus(hWnd, hWndPrevFocus)
HWND hWnd;
HWND hWndPrevFocus;
{
DestroyCaret();
CreateCaret( hWndTerm, NULL, char_w, char_h );
SetCaretPos( curs_pos.x, curs_pos.y );
TermHasFocus = TRUE;
TermShowCaret( TRUE );
}
TermLoseFocus(hWnd, hWndNewFocus)
HWND hWnd;
HWND hWndNewFocus;
{
DestroyCaret();
TermHasFocus = FALSE;
}
TermMouse(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
POINT lParam;
{
}
TermMove(hWnd, x, y)
HWND hWnd;
int x, y;
{
int i; /* Save the cords. of the window */
GetWindowRect(hWnd,(LPRECT)&rect);
if ((rect.right-rect.left > GetSystemMetrics(SM_CXICON)+10) ||
(rect.bottom-rect.top > GetSystemMetrics(SM_CYICON)+10)) {
CopyRect((LPRECT)&win_sz.size,(LPRECT)&rect);
}
return( TRUE );
}
TermPaint( hWnd )
HWND hWnd;
{
int i,j;
HDC hDC;
HFONT htmp;
hDC = BeginPaint (hWnd, (LPPAINTSTRUCT) &ps);
if ( PaintWindow ) { /* Window is not painted after a scroll, saves time */
/****************************************
* if window is not open, don't paint *
****************************************/
if ((no_lines == 0) || (win_size == SIZEICONIC)) {
EndPaint (hWnd, (LPPAINTSTRUCT) &ps);
return(0L);
}
/*********************************
* paint window *
*********************************/
htmp = SelectObject( hDC, IBMfont );
i = ps.rcPaint.top / char_h;
for(;i<no_lines;i++) {
for ( j = no_cols-1; j > 0 && *(display[i].string+j) == ' '; j-- );
TextOut(hDC,char_w,((char_h*i)?(char_h*i):1),(LPSTR)display[i].string,++j);
}
} /* End if ( PaintWindow ) */
PaintWindow = TRUE;
EndPaint(hWnd,(LPPAINTSTRUCT) &ps);
}
TermQueryEndSession(hWnd)
HWND hWnd;
{
int id;
char szOkSave[CCHMAXSTRING], szSave[CCHMAXSTRING];
#if 0 /* Disabled */
LoadString(hInstTerm, IDSOKSAVE, (LPSTR)szOkSave, CCHMAXSTRING);
LoadString(hInstTerm, IDSSAVE , (LPSTR)szSave , CCHMAXSTRING);
MessageBeep(MB_OKCANCEL | MB_ICONQUESTION);
id = MessageBox(hWnd, (LPSTR)szOkSave, (LPSTR)szSave,
MB_YESNOCANCEL | MB_ICONQUESTION);
switch (id)
{
case IDYES:
/* save files etc and fall through */
case IDNO:
/* don't save but still permit End Eession to continue */
return TRUE;
case IDCANCEL:
/* don't allow End Session to continue */
return FALSE;
}
#endif
}
TermSize(hWnd, x, y, code, lParam)
HWND hWnd;
int x, y;
int code;
long lParam;
{
int h, w;
long lng;
TEXTMETRIC tm;
win_sz.icon = FALSE;
DestroyCaret();
switch (code) {
case SIZEICONIC:
if (win_size != SIZEICONIC) {
/****************************************
* reset variables to reflect status *
****************************************/
win_size = SIZEICONIC;
win_sz.icon = TRUE;
/****************************************
* set the timer to monitor the serial *
* port while program is iconic *
****************************************/
KillTimer(hWndTerm,OPEN_TIMER);
SetTimer(hWndTerm,ICON_TIMER,ICON_RATE, lpprocTimer);
}
break;
case SIZEFULLSCREEN:
win_size = SIZENORMAL;
*buff = 0x11; /* CONTROL Q: Start Transmission again, just for kicks */
ret = WriteComm( nCID, (LPSTR) buff, 1 );
if ( flashing ) {
flashing = FALSE;
KillTimer(hWndTerm,FLASH_TIMER);
SetTimer(hWndTerm,OPEN_TIMER,OPEN_RATE,NULL);
}
break;
case SIZENORMAL:
win_size = SIZENORMAL;
if ( no_cols < 1 || no_cols > MAX_COLS ) no_cols = 80; /* Check */
if ( no_lines < 1 || no_lines > MAX_LINES ) no_lines = 24; /* Bounds */
w = LOWORD(lParam) / no_cols; /* Determine best fit cursor values */
h = HIWORD(lParam) / no_lines;
w = (w < 1 ? 1 : w ); /* Check bounds */
w = (w > 20 ? 20 : w );
h = (h < 1 ? 1 : h );
h = (h > 20 ? 20 : h );
if ( IBMfont ) /* remove current font */
DeleteObject( IBMfont );
/* Create best fit font */
IBMfont = CreateFont( h, w,0,0,0,0,0,0,
OEM_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH|FF_DONTCARE, (LPSTR) "IBM" );
if ( IBMfont == NULL ) { /* Unable to load font */
pmsg("Unable to select font.");
IBMfont = GetStockObject( SYSTEM_FONT );
}
SelectObject( hTermDC, IBMfont ); /* Select font */
/* Get new fonts values. NOTE: Font created may NOT match the */
/* values asked for. Windows gives the closest match. */
GetTextMetrics (hTermDC, (TEXTMETRIC FAR *)&tm); /* text size */
char_w = tm.tmAveCharWidth; /* char width */
char_h = tm.tmHeight + tm.tmExternalLeading; /* char height */
w = char_w * (no_cols + 6); /* Size window acording to new font */
h = char_h * (no_lines + 6);
GetWindowRect(hWnd,(LPRECT)&rect);
MoveWindow( hWndTerm, rect.left, rect.top, w, h, TRUE);
if ((rect.right-rect.left > GetSystemMetrics(SM_CXICON)+10) ||
(rect.bottom-rect.top > GetSystemMetrics(SM_CYICON)+10)) {
CopyRect((LPRECT)&win_sz.size,(LPRECT)&rect);
}
if ( flashing ) {
flashing = FALSE;
KillTimer(hWndTerm,FLASH_TIMER);
SetTimer(hWndTerm,OPEN_TIMER,OPEN_RATE,NULL);
}
break;
}
PaintWindow = TRUE;
GetWindowRect(hWnd, (LPRECT)&win_sz.size); /* get screen size */
CreateCaret( hWndTerm, NULL, char_w, char_h );
curs_pos.x = char_w * (next_char+1);
curs_pos.y = char_h * next_line;
TermShowCaret( TRUE );
return( TRUE );
}
long TermCommand(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
int i;
HMENU commands;
switch (wParam) {
case IDMFILE: /* Select Configuration file, open comm port */
ret = DialogBox(hInstTerm,MAKEINTRESOURCE(PATHBOX),hWndTerm,lpprocSelectDlg);
if ( ret && ReadConfig() == FALSE )/* Read the termcap - .cnf file */
return(FALSE);
if ( TermOpenComm( ) == FALSE ) /* Open the comm port */
return(FALSE);
*buff = CR;
WriteComm( nCID, (LPSTR) buff, 1 ); /* Jump start the port */
EnableMenuItem( hMenuMain, 1, MF_BYPOSITION|MF_ENABLED );
/****************************************
* Put a message in the queue *
* to go and look for a response *
****************************************/
PostMessage(hWndTerm,WM_TIMER,OPEN_TIMER,(LONG)NULL);
/****************************************
* Set the timer to monitor the serial *
* port *
****************************************/
ret = SetTimer(hWndTerm,OPEN_TIMER,OPEN_RATE,NULL);
if (!ret) { /* error starting timer */
pmsg("Error starting open timer");
PostMessage(hWndTerm,WM_CLOSE,NULL,(LONG)NULL);/* post closing msg */
}
break;
case IDMCONNECT: /* Connect to comm port - disabled */
break;
case IDMDIALOG: /* Show the message dialog window */
ShowWindow(hWdesc, SW_NORMAL);
break;
case IDMCONFIG: /* Show the configuration window */
DialogBox(hInstTerm,MAKEINTRESOURCE(CONFIGTERM),hWnd,lpprocConfigTerm);
break;
case IDMCLEAR: /* Clear the display */
TermShowCaret( FALSE );
next_char = 0;
next_line = 0;
curs_pos.x = char_w;
curs_pos.y = 1;
GraphicsMode = FALSE;
SetCaretPos( curs_pos.x, curs_pos.y );
for ( i = 0; i < no_lines; i++ )
memset( display[i].string, ' ', no_cols );
GetClientRect( hWndTerm, (LPRECT) &rect );
FillRect( hTermDC, (LPRECT) &rect, GetStockObject(WHITE_BRUSH));
TermShowCaret( TRUE );
break;
case IDMON: /* Turn capture mode on */
commands = GetSubMenu( hMenuMain, 2 );
if ( Capture == FALSE )
OpenCapture();
CheckMenuItem( commands, IDMON, MF_CHECKED );
CheckMenuItem( commands, IDMOFF, MF_UNCHECKED );
Capture = TRUE;
break;
case IDMOFF: /* Turn capture mode off */
if ( Capture == TRUE )
CloseCapture();
commands = GetSubMenu( hMenuMain, 2 );
CheckMenuItem( commands, IDMON, MF_UNCHECKED );
CheckMenuItem( commands, IDMOFF, MF_CHECKED );
Capture = FALSE;
break;
default:
return(DefWindowProc(hWnd, message, wParam, lParam));
break;
}
return( 0L );
}
long TermSysCommand(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
switch (wParam) {
case IDMABOUT:
DialogBox(hInstTerm, MAKEINTRESOURCE(ABOUTDIALOG), hWnd,
lpprocTermAboutDlg);
return TRUE;
break;
case IDMSAVEWINDOW: /* Save the window size */
LoadString( hInstTerm, IDSWINDOWNAME, (PSTR) win_sz.app_name, 30 );
ret = SaveWindowSize(&win_sz);
if (ret)
MessageBox(hWndTerm,(LPSTR)"Error saving coordinates",NULL,MB_OK);
return TRUE;
break;
/* N.B MUST HAVE A CALL TO DefWindowProc HERE !!!!! */
default:
return(DefWindowProc(hWnd, message, wParam, lParam));
break;
}
return (0L);
}
TermTimer( hWnd, TimerID )
HWND hWnd;
WORD TimerID;
{
int size_read;
unsigned int u;
char port_char;
COMSTAT CommStatus;
if (TimerID == OPEN_TIMER) {
if (win_size == SIZENORMAL) { /* make sure window is open */
u = GetCommError( nCID, (COMSTAT *) &CommStatus );
if ( CommStatus.cbInQue ) /* A Character is in the receive queue */
ReadLine( ); /* Read from the port */
} /* End if(win_size == SIZENORMAL) */
else
KillTimer(hWndTerm,OPEN_TIMER);
} /* End if(wParam == OPEN_TIMER) */
/*****************************************
* process the timer that is set to flash *
* the icon, it is set when data is *
* waiting to be displayed and the *
* program is iconic *
*****************************************/
else if ((TimerID == FLASH_TIMER) && (flashing)) {
if (win_size == SIZEICONIC) {
if (message_action == MSG_FLASH) {
GetWindowRect(hWndTerm,(LPRECT)&rect);
FlashWindow(hWndTerm,TRUE);
}
else if (message_action == MSG_BEEP) {
Beep();
GetWindowRect(hWndTerm,(LPRECT)&rect);
FlashWindow(hWndTerm,TRUE);
}
else if (message_action == MSG_OPEN) {
KillTimer(hWndTerm,FLASH_TIMER);
SetTimer(hWndTerm,OPEN_TIMER,OPEN_RATE,NULL);
ShowWindow( hWndTerm, SW_RESTORE );
}
else if (message_action == MSG_DISP) {
KillTimer(hWndTerm,FLASH_TIMER);
MessageBox(hWndTerm,
(LPSTR)" THERE IS A MESSAGE FROM THE ICU - PLEASE OPEN TERMINAL ICON ",
(LPSTR)" ATTENTION!!! ",MB_ICONHAND|MB_OK);
SetTimer(hWndTerm,FLASH_TIMER,FLASH_RATE * 3,NULL);
}
}
else {
KillTimer(hWndTerm,FLASH_TIMER);
flashing = FALSE;
}
}
}
/**/
/*doc************************************************************************
*
* NAME
*
* Timer() - processes the timer interrupt
*
* FORMAT
*
* BOOL FAR PASCAL Timer(hWnd, message, wParam, lParam)
* HWND hWnd;
* unsigned message;
* WORD wParam;
* LONG lParam;
*
* DESCRIPTION
* When the timer interrupts, this function is called. The timer is
* only set when the program is iconic. When it goes off, this function
* goes out and reads the serial port to see if there is any message
* waiting from the unix box. If there is, the entire line that is
* waiting is read (if no errors), and the action determined by the
* message variable is taken. The possible values for this are
* FLASH - this means to flash the icon
* BEEP - flash the icon and sound the beeper
* OPEN - open a message box informing the user
*
* This timer that polls the port is killed since it is no longer
* necessary once data has been found. If an error is found while
* reading, a message box is brought up to that effect, but the
* program continues on after the error is acknowledged.
*
* RETURN VALUE
*
* MODIFIES
* flashing
*
* REFERENCES
* ReadComm()
* DisplayCommError()
* ReadLine()
*
*end************************************************************************/
BOOL FAR PASCAL Timer(hWnd, message, wParam, lParam)
HWND hWnd;
unsigned message;
WORD wParam;
LONG lParam;
{
/****************************************
* local variable declarations *
****************************************/
int ret;
unsigned int u;
COMSTAT CommStatus;
/****************************************
* see if anything at port *
****************************************/
u = GetCommError( nCID, (COMSTAT *) &CommStatus );
if ( CommStatus.cbInQue ) {
return( FALSE ); /* no bytes were read from port */
} /* keep going */
/****************************************
* if the return was not zero than there *
* is something at the port - if so, *
* read till the end of the line and *
* notify the user that there is some *
****************************************/
KillTimer(hWndTerm,ICON_TIMER); /* kill poll timer since data found */
ret = ReadLine( );
if (ret)
DisplayCommError();
if (!flashing) {
SetTimer(hWndTerm,FLASH_TIMER,FLASH_RATE,NULL);
flashing = TRUE;
return(TRUE);
}
return(FALSE);
}